package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.UserUpdateBean;
import cst.neu.yuejian_31.iface.UserUpdateIface;
import cst.neu.yuejian_31.iface.UserUpdateListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.UserUpdateService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/17.
 */

public class UserUpdateModel implements UserUpdateIface  {
    private Retrofit retrofit;
    public UserUpdateModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void userUpdate(String userId, String uname,String upass, String uphone,final UserUpdateListener userUpdateListener) {
        UserUpdateService userUpdateService=retrofit.create(UserUpdateService.class);
        Call<UserUpdateBean>call=
                userUpdateService.userUpdate(userId,upass,uname,uphone);
        call.enqueue(new Callback<UserUpdateBean>() {
            @Override
            public void onResponse(Call<UserUpdateBean> call, Response<UserUpdateBean> response) {
              if(response.isSuccessful()&&response.body()!=null){
                  userUpdateListener.onResponse(response.body());
              }else {
                  userUpdateListener.onFail("FAIL");
              }
            }

            @Override
            public void onFailure(Call<UserUpdateBean> call, Throwable t) {

                userUpdateListener.onFail(t.toString());
            }
        });
    }
}
